package com.atguigu.t_juc_help_class;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

class MySources{
    int num;
    public synchronized void incrementNum(){
        num++;
        System.out.println("已经集齐了第..."+num+"...颗龙珠");
    }
}

public class MyCyclicBarrier {
    public static void main(String[] args) {
        MySources mySources = new MySources();

        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙。。。。。");
        });

        for (int i = 0; i < 7; i++) {
            new Thread(() -> {
                mySources.incrementNum();
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
